import 'dart:math';

import 'package:flutter/material.dart';
import 'package:jump_scape/js_data/js_coin_shop.dart';
import 'package:jump_scape/js_ui/js_activity/js_coin_shop.dart';
import 'package:jump_scape/js_ui/js_activity/js_game.dart';
import 'package:jump_scape/js_ui/js_dialog/js_loading.dart';
import 'package:jump_scape/js_ui/js_dialog/js_topic.dart';
import 'package:jump_scape/js_ui/js_widget/js_blur_group.dart';
import 'package:jump_scape/js_ui/js_widget/js_image_view.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:jump_scape/js_ui/js_widget/js_style.dart';
import 'package:jump_scape/js_ui/js_widget/js_title_view.dart';

import '../../js_data/js_box/js_init.dart';
import '../../js_data/js_event.dart';
import '../../objectbox.g.dart';

class JsTopicActivity extends StatefulWidget {
  static const url = 'topic';
  const JsTopicActivity({super.key});

  @override
  State<StatefulWidget> createState() => _JsTopicActivity();
}

class _JsTopicActivity extends State<JsTopicActivity> {
  final _unlockList = [0, 1];

  @override
  void initState() {
    super.initState();
    JsEventManager.statPage(page: JsEventName.jsNameTopic);

    _init();
  }

  _init() async {
    final setting = await JsObjectBox.setting;
    var member = JsObjectBox.memberBox.query(MemberBox_.userId.equals(setting.userId)).build().findFirst();
    if (member != null) {
      if (member.isUnlockTopic3) {
        if (!_unlockList.contains(2)) {
          _unlockList.add(2);

          if (mounted) setState(() {});
        }
      }
    }
  }

  @override
  Widget build(BuildContext context) {
    return WillPopScope(
      onWillPop: () => Future.value(false),
      child: Scaffold(
        body: Stack(
          children: [
            jsImage('assets/image/ic_topic_bg.png', width: double.infinity, height: double.infinity, fit: BoxFit.cover),
            Column(
              children: [
                JsTitleView(title: 'Topic Selection'),
                Expanded(
                  child: JsBlurGroup(
                    width: double.infinity,
                    height: double.infinity,
                    radius: 20.r,
                    margin: EdgeInsets.fromLTRB(22.r, 11.r, 22.r, ScreenUtil().bottomBarHeight + 48.r),
                    child: ListView.separated(
                      padding: EdgeInsets.symmetric(horizontal: 16.r, vertical: 33.r),
                      itemCount: 3,
                      itemBuilder: (context, index) {
                        return Container(
                          decoration: jsShapeRound(
                            radius: 30.r,
                            shadows: [
                              BoxShadow(offset: Offset(2.r, 4.r), color: Color(0xFFAE6060).withOpacity(0.12), blurRadius: 5.r),
                            ],
                          ),
                          child: InkWell(
                            child: Container(
                              clipBehavior: Clip.antiAlias,
                              decoration: jsShapeRound(radius: 30.r),
                              child: Stack(
                                children: [
                                  jsImage('assets/image/ic_topic_item_${index + 1}.png', width: double.infinity, fit: BoxFit.fitWidth),
                                  Positioned(
                                    right: 0,
                                    bottom: 0,
                                    child: jsImage(isUnlock(index) ? 'assets/image/ic_topic_unlock.png' : 'assets/image/ic_topic_lock.png', height: 36.r, fit: BoxFit.fitHeight),
                                  ),
                                ],
                              ),
                            ),
                            onTap: () {
                              if (isUnlock(index)) {
                                Navigator.popAndPushNamed(context, JsGameActivity.url, arguments: index);
                              } else {
                                jsShowBuyTopicDialog(context, coins: '56', onTap: (isYes) {
                                  if (isYes) {
                                    jsShowLoadingDialog(context);

                                    JsCoinShop().consumption(
                                      coin: '56',
                                      call: (isSucc, isInsufficient, msg) {
                                        Navigator.pop(context);

                                        if (isSucc) {
                                          JsObjectBox.setting.then((setting) {
                                            var member = JsObjectBox.memberBox.query(MemberBox_.userId.equals(setting.userId)).build().findFirst();
                                            if (member != null) {
                                              member.isUnlockTopic3 = true;
                                              JsObjectBox.memberBox.put(member);
                                            }
                                          });

                                          jsShowPurchaseSuccessDialog(context, () {});

                                          _unlockList.add(index);

                                          setState(() {});
                                        } else {
                                          if (isInsufficient) {
                                            jsShowInsufficientGoldCoinsDialog(context, (isRecharge) {
                                              if (isRecharge) {
                                                Navigator.pushNamed(context, JsCoinShopActivity.url);
                                              }
                                            });
                                          } else {
                                            if (msg.isNotEmpty) {
                                              ScaffoldMessenger.of(context).showSnackBar(
                                                SnackBar(
                                                  content: Text(msg, style: jsTextStyle(font: JsTextFont.PoetsenOne, fontColor: Colors.white, fontSize: 20.r)),
                                                ),
                                              );
                                            }
                                          }
                                        }
                                      },
                                    );
                                  }
                                });
                              }
                            },
                          ),
                        );
                      },
                      separatorBuilder: (BuildContext context, int index) {
                        return SizedBox(height: 25.r);
                      },
                    ),
                  ),
                ),
              ],
            ),
          ],
        ),
      ),
    );
  }

  bool isUnlock(int index) {
    return _unlockList.contains(index);
  }
}
